package com.cmy.web.activity

import android.content.Context
import android.content.Intent
import android.os.Bundle
import android.view.View
import com.cmy.framework_base.basics.BasicsActivity
import com.cmy.framework_base.common.UiState
import com.cmy.framework_base.common.loading.SimpleLoadDialogImp
import com.cmy.framework_utils.JumpPageUtils
import com.cmy.web.KEY_TITLE
import com.cmy.web.KEY_URL
import com.cmy.web.WebViewState
import com.cmy.web.databinding.ActivityWebBinding
import com.cmy.web.databinding.ActivityWebX5Binding
import com.cmy.web.listener.WebViewStateListener
import kotlinx.coroutines.DelicateCoroutinesApi
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.GlobalScope
import kotlinx.coroutines.flow.MutableStateFlow
import kotlinx.coroutines.launch

/**
 * @name:
 *  WebViewActivity
 * @package:
 *  com.cmy.web.activity
 * @date:
 *  2023/4/2615:36
 * @author
 *    cmy
 * @description
 *
 */
class WebViewActivity : BasicsActivity(), WebViewStateListener {

    private val binding by lazy { ActivityWebBinding.inflate(layoutInflater) }

    override fun getContentView(): View = binding.root

    private var title:String? = null

    private var url : String? = null

    private  val mWebView by lazy {binding.webView}


    private val uiState = MutableStateFlow(UiState.LoadDefault)

    override fun doIntent(intent: Intent) {
        title = intent.getStringExtra(KEY_TITLE)
        url = intent.getStringExtra(KEY_URL)
    }


    override fun initView(bundle: Bundle?) {
        registerLoadSir(binding.root)
        refreshUiState(uiState)
        mWebView.registerWebViewStateListener(this)
    }

    override fun initData() {

        onReloadClick()
    }


    override fun onReloadClick() {
            url?.let {
                mWebView.loadUrl(it)
            }
    }

    override fun onState(state: WebViewState) {
        when(state){
            WebViewState.Loading -> uiState.value = UiState.Loading
            WebViewState.LoadEnd -> uiState.value = UiState.LoadEnd
            WebViewState.LoadError -> {
                uiState.value = UiState.LoadError
                showFailure("网络加载错误")
            }
            WebViewState.LoadComplete -> uiState.value = UiState.LoadComplete
            WebViewState.LoadDefault -> uiState.value = UiState.LoadDefault
        }
    }
}